import type {IColumnsOption} from 'vc';
import {timestampToTime} from '@vc/utils';
import {
  INDEX,
  COMPANY_NAME,
  BUSINESS_DATA,
  REGISTERED_CAPITAL,
  PROVINCE,
  CITY,
  COUNTRY,
  INDUSTRY_CATEGORY,
  CellRenderMap
} from '@/utils/tableColumn';
import {setHrefUrlTool, toF9} from "@/utils";

export function useTable(data) {
  const totalSize = computed(() => {
    return data?.value?.total ?? 0;
  });
  const columns = ref<IColumnsOption[]>([
    {...INDEX},
    {
      field: 'noticeDate',
      title: '发生时间',
      align: 'center',
      sortable: true,
      width: 110,
      sortBy: 'noticeDate',
      formatter: ({row}) => {
        return timestampToTime(row.eventDate)
      }
    },
    {
      field: 'eventTypeLabels',
      title: '事件类型',
      align: 'center',
      width: 110,
      slots: {
        default: ({row}) => {
          if (row.eventTypeLabels?.length === 0) {
            return '-';
          }
          return row?.eventTypeLabels?.map((item) => {
            return (
              <div>
                {item.name}
              </div>
            );
          });
        }
      }
    },
    {
      field: 'attachments',
      title: '标题',
      slots: {
        default: ({row}) => {
          return (
            <div>
              <a onClick={() => setHrefUrlTool(row.fileType, row.fileUrl, row.id, row.bizType)}>
                {row.eventTitle} <span
                style={'color:#777;' + 'display:' + (row.eventProcess.length > 1 ? 'initial' : 'none')}>[进展 {row.eventProcess.length}]</span>
              </a>
              <br/>
            </div>
          );
        }
      }
    },
    {
      field: 'riskLabel',
      title: '风险等级',
      align: 'center',
      sortable: true,
      width: 110,
      sortBy: 'risk',
      slots: {
        default: ({row}) => {
          return (
            <div style={'color:' + row.riskLabel.color + ';background:' + row.riskLabel.bgColor + ';width:40px;margin:0 auto;text-align:center;'}>
              {row.riskLabel.name}
            </div>
          );
        }
      }
    },
    {
      field: 'relatedCompany',
      title: '相关企业',
      align: 'center',
      width: 300,
      slots: {
        default: ({row}) => {
          if (row.relatedCompany?.length === 0) {
            return '-';
          }
          return row?.relatedCompany?.map((item) => {
            return (
              <div>
                <a onClick={() => toF9(item.id)}>{item.name}</a>
                <br/>
              </div>
            );
          });
        }
      }
    },
    {
      field: 'source',
      title: '来源',
      align: 'center',
      width: 250,
    },
  ]);

  return {
    totalSize,
    columns
  };
}
